package cardwall.client.view;

import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.mediator.Mediator;
import cardwall.client.ApplicationFacade;
import cardwall.client.model.TasksProxy;
import cardwall.client.view.HeaderPanel;
import cardwall.shared.Task;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author $LastChangedBy: vogensen $
 * @version $Revision: 2 $
 */
public class HeaderPanelMediator extends Mediator {
    public HeaderPanelMediator(HeaderPanel headerPanel) {
        super("HeaderPanelMediator", headerPanel);
        headerPanel.init();
        getHeaderPanel().addNewButtonClickListener(new ClickListener() {
            public void onClick(Widget widget) {
                TasksProxy tasksProxy = (TasksProxy) ApplicationFacade.getInstance().retrieveProxy(TasksProxy.PROXYNAME);
                tasksProxy.createTask();
            }
        });
        getHeaderPanel().addEditButtonClickListener(new ClickListener() {
            public void onClick(Widget widget) {
                sendNotification(ApplicationFacade.EDITTASK, getHeaderPanel().getSelectedTask(), ApplicationFacade.NOTIFICATIONTYPE_DEFAULT);
            }
        });
    }

    private HeaderPanel getHeaderPanel() {
        return (HeaderPanel) viewComponent;
    }

    public String[] listNotificationInterests() {
        return new String[] {ApplicationFacade.SELECTION_CHANGED};
    }

    public void handleNotification(INotification inotification) {
        if(inotification.getName().equals(ApplicationFacade.SELECTION_CHANGED)) {
            // change button state based on state in body()
            getHeaderPanel().setSelectedTask((Task) inotification.getBody());
        }
    }
}